## -------------------------------- Code SpringerPlus Paper ---------------------
require("tm")
require("smacof")
require("proxy")
require("ggplot2")
require("colorspace")
require("ape")
require("igraph")
ps.options(fonts = 'serif')
load("dtmred.rda")
load("statementPairs.rda")

## -------------------------------------- MDS ----------------------------------
## degenerate solution
eqdiss <- matrix(1, ncol = 35, nrow = 35)
reseqdiss <- smacofSym(eqdiss)
postscript(file = "figure1.eps", width = 5, height = 5)
plot(reseqdiss, label.conf = list(FALSE), xlab = "", ylab = "", main = "MDS Constant Dissimilarities")
dev.off()

## gravity
Y <- dt.mat.red
Y[Y > 0] <- 1
co.maty <- t(Y)%*%Y       
grav.disty <- sqrt(rowSums(co.maty)%*%t(colSums(co.maty))/co.maty)  
rownames(grav.disty) <- colnames(grav.disty)
grav.disty1 <- grav.disty
W <- matrix(1, ncol = ncol(grav.disty), nrow = nrow(grav.disty))
W[grav.disty1 == Inf] <- 0              
grav.disty[grav.disty1 == Inf] <- 1000   
lambda <- 2                              
rescoy <- smacofSym(grav.disty^lambda, weightmat = as.dist(W))
clustdist <- rescoy$confdiss
mdsclust <- hclust(clustdist, method = "ward.D")             ## hierarchical cluster analysis
clus6 <- cutree(mdsclust, 6)              
gg_color_hue <- function(n) {
   hues = seq(15, 375, length=n+1)
   hcl(h=hues, l=65, c=100)[1:n]
}
mypal <- gg_color_hue(6)
Clusters <- factor(clus6, labels = c("Civil Liberties I", "Traditional Morality", "Patriotism, Traditional Morality", "Civil Liberties II", "Military, Law, and Order", "Free Enterprise"))
confs <- data.frame(rescoy$conf, Clusters)
postscript(file = "figure2.eps")
ggMDS <- ggplot(confs, aes(x = D1,y = D2, label = row.names(confs), color=Clusters)) + xlab("") + ylab("") + xlim(-1.2, 1.2)
print(ggMDS + geom_point() 
             + geom_text(size = 4, vjust = -1)
             + ggtitle("MDS Configurations Republican Values")
             + guides(colour=guide_legend(title = "Republican Subgroups", override.aes = list(size = 3))))
dev.off()
postscript(file = "figure3.eps")
op1 <- par(bg = "white", mar=c(1,1,1,1))
plot1 <- plot(as.phylo(mdsclust), type = "fan", tip.color = mypal[Clusters],
               label.offset = 0.1, cex = 0.65, col = "red", y.lim = c(-6.5, 6.5), x.lim = c(-6.5,6.5))
par(op1)
dev.off()

## ------------------------------- network analysis -----------------------------
SPsub <- statementPairs 
statementGraph <- graph.edgelist(SPsub, directed = TRUE) 
E(statementGraph)$weight <- count.multiple(statementGraph)
statementGraph <- simplify(statementGraph, edge.attr.comb = list(weight=max,name="concat", "ignore"))
set.seed(123)
ly <- layout.fruchterman.reingold(statementGraph)    
xc <- walktrap.community(statementGraph, weights = E(statementGraph)$weight, step = 6) 

edgeList <- tapply(seq_along(membership(xc)), membership(xc), function(xx) xx)
comList <- tapply(membership(xc), membership(xc), names)                      
comsize <- sapply(comList, length)
bigComIndex <- which(comsize > 20)
coms3 <- edgeList[bigComIndex[[1]]]
coms4 <- edgeList[bigComIndex[[3]]]
smallComIndex <- which(comsize > 6 & comsize < 20)
smallComIndex1 <- smallComIndex[6]
smallComIndex2 <- smallComIndex[9]


## ------------- full plot --------------
postscript(file = "figure4.eps")
vcolo <- c(hcl(h=0,35,60),hcl(h=72,c=35,l=60),hcl(h=144,c=35,l=60),hcl(h=216,c=35,l=60),hcl(h=320,c=35,l=60))
op <- par(mar = c(1,2,1,1))
layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
plot(statementGraph, layout = ly, vertex.size = 2, edge.color = "lightgray", 
             edge.arrow.size = 0.05, edge.curved = FALSE, 
             vertex.label = NA, 
             rescale = FALSE, 
             xlim = c(-530, 630), ylim = c(-440, 700),
             main= "Republican Statements", asp = 0,
             margin = -1)
comGraphBig1 <- induced.subgraph(statementGraph, coms3[[1]])
ly1 <- ly[unlist(coms3[[1]]),] 
plot(comGraphBig1, layout = ly1, vertex.size = 3, edge.color = vcolo[2],
             edge.arrow.size = 0.2, edge.curved = FALSE,  vertex.label = NA, 
             vertex.label.dist = 0, vertex.label.cex = 0.8, vertex.label.font = 2,
             rescale = FALSE, add=TRUE, vertex.color=vcolo[2],vertex.frame.color=vcolo[2],vertex.label.color=vcolo[2])
comGraphBig2 <- induced.subgraph(statementGraph, coms4[[1]])
ly1 <- ly[unlist(coms4[[1]]),]
plot(comGraphBig2, layout = ly1, vertex.size = 3, edge.color = vcolo[3], 
             edge.arrow.size = 0.2, edge.curved = FALSE,  vertex.label = NA, 
             vertex.label.dist = 0, vertex.label.cex = 0.8, vertex.label.font = 2,
             rescale = FALSE, add=TRUE, vertex.color=vcolo[3],vertex.frame.color=vcolo[3],vertex.label.color=vcolo[3])
op2 <- par(mar = c(4,1,1,1))
colo <- c(hcl(200,35,40),hcl(200,35,80))
comGraphSmall1 <- induced.subgraph(statementGraph, unlist(edgeList[smallComIndex1]))
ly1 <- ly[unlist(edgeList[smallComIndex1]),]     
op1 <- par(mar = c(1,2,1,2))
plot(comGraphSmall1, layout = ly1, vertex.size = 2, edge.color = colo[2], 
             edge.arrow.size = 0.50, edge.curved = FALSE, edge.width = E(comGraphSmall1)$weight,
             vertex.label.dist = 30, vertex.label.cex = 0.8, vertex.label.font = 2,
             vertex.color=colo[1],vertex.label.color=colo[1], 
             rescale = FALSE, xlim=range(ly1[,1]), ylim=range(ly1[,2]))
colo <- c(hcl(320,35,40),hcl(320,35,80))
par(op1)
comGraphSmall2 <- induced.subgraph(statementGraph,unlist(edgeList[smallComIndex2]))
ly1 <- ly[unlist(edgeList[smallComIndex2]),]  
op1 <- par(mar = c(1,2,1,2))
plot(comGraphSmall2, layout = ly1, vertex.size = 2, edge.color = colo[2], 
           edge.arrow.size = 0.5, edge.curved = FALSE, edge.width = E(comGraphSmall2)$weight,
             vertex.label.dist = 30, vertex.label.cex = 0.8, vertex.label.font = 2,
             vertex.color=colo[1],vertex.label.color=colo[1], 
             rescale = FALSE, xlim=range(ly1[,1]), ylim=range(ly1[,2]))
par(op1)
par(op2)
par(op)
dev.off()
  
## --------------- large network communities -------------
op <- par(mfrow = c(2,1), mar = c(1,1,1,1))
colo <- c(hcl(72,35,40),hcl(72,35,80))
comGraphBig1 <- induced.subgraph(statementGraph, unlist(edgeList[bigComIndex[1]]))
ly1 <- ly[unlist(edgeList[bigComIndex[1]]),] 
plot(comGraphBig1, layout = ly1, vertex.size = 2, edge.color = colo[2],
             vertex.color=colo[1],vertex.label.color=colo[1],
             edge.arrow.size = 0.8, edge.curved = FALSE, edge.width = E(comGraphBig1)$weight,
             vertex.label.dist = 30, vertex.label.cex = 1.2, vertex.label.font = 2,
             rescale = FALSE, xlim=range(ly1[,1]), ylim=range(ly1[,2]))
colo <- c(hcl(144,35,40),hcl(144,35,80))
comGraphBig2 <- induced.subgraph(statementGraph, unlist(edgeList[bigComIndex[3]]))
ly1 <- ly[unlist(edgeList[bigComIndex[3]]),] 
plot(comGraphBig2, layout = ly1, vertex.size = 2, edge.color = colo[2],
             vertex.color=colo[1],vertex.label.color=colo[1],
             edge.arrow.size = 0.8, edge.curved = FALSE, edge.width = E(comGraphBig2)$weight,
             vertex.label.dist = 1, vertex.label.cex = 1.2, vertex.label.font = 2,
             rescale = FALSE, xlim=range(ly1[,1]), ylim=range(ly1[,2]))
par(op)
  

